import { createStore } from './redux'
function reducer(state, action) {
  switch (action.type) {
    case 'add':
      return { ...state, count: state.count + 1 }
    case 'minus':
      return { ...state, count: state.count - 1 }
    default:
      return state
  }
}
const store = createStore(reducer, { count: 0 })

function render() {
  const state = store.getState()
  const root = document.getElementById('root')
  root.textContent = state.count
}

const addBtn = document.getElementById('addBtn')
const minusBtn = document.getElementById('minusBtn')

addBtn.addEventListener('click', () => {
  store.dispatch({ type: 'add' })
})

minusBtn.addEventListener('click', () => {
  store.dispatch({ type: 'minus' })
})

render()
store.subscribe(render)
